package com.intelligentmeterreadingsys.waterlist.reviewdata.activity;

import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.UseWaterInfoPoVo;
import com.intelligentmeterreadingsys.povo.UserWaterVO;
import com.intelligentmeterreadingsys.report.activity.ReportNoRFIDActivity;
import com.intelligentmeterreadingsys.usermanager.server.AChartEngine;
import com.intelligentmeterreadingsys.waterbanding.activity.WaterBandingMainActivity;
import com.intelligentmeterreadingsys.waterlist.db.WaterListDaoImpl;
import com.intelligentmeterreadingsys.waterlist.querywaterlist.activity.QueryRoutePlanMap;
import com.intelligentmeterreadingsys.waterlist.reviewdata.db.ReviewDataDao;

/**
 * 复查数据水表信息
 * 
 * @author king
 * 
 */
public class ReviewWaterinfoActivity extends Activity {
	public static final int ITEM0 = Menu.FIRST;// 系统值
	public TextView textview1;// 用户姓名
	public TextView textview2;// 用户编号
	public TextView textview3;// 用户电话
	public TextView textview4;// 水表编号
	public TextView textview5;// 水表地址
	public TextView textview6;// 用水性质
	public TextView textview7;// 上次抄表刻度
	public TextView textview8;// 上次抄表时间
	public TextView textview9;// 本次抄表刻度
	public TextView textview10;// 抄表状态
	private Bundle bundle = null;// 条件对象
	public UserWaterVO water = null;
	public String waterid = null;
	public Button button3;
	public ReviewDataMainActivity act = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.waterinfo);
		TextView textview = (TextView) findViewById(R.id.textViewwaterinfo2);
		textview.setText("水表及复抄信息");
		inti();

	}

	/**
	 * 初始化控件
	 */
	public void inti() {
		textview1 = (TextView) findViewById(R.id.waternumber);
		textview2 = (TextView) findViewById(R.id.watervalue);
		textview3 = (TextView) findViewById(R.id.otherwatervalue);
		textview4 = (TextView) findViewById(R.id.lastwaternumber);
		textview5 = (TextView) findViewById(R.id.lastdate);
		textview6 = (TextView) findViewById(R.id.waterproperties);
		textview7 = (TextView) findViewById(R.id.wateradds);
		textview8 = (TextView) findViewById(R.id.tonnage);
		textview9 = (TextView) findViewById(R.id.waterlist_copystatus);
		textview10 = (TextView) findViewById(R.id.waterlist_user_phone);

	}

	/**
	 * 添加数据
	 * 
	 * @param b
	 * @param usernumber
	 */
	public void addDate() {
		textview1.setText("");
		textview2.setText("");
		textview3.setText("");
		textview4.setText("");
		textview5.setText("");
		textview6.setText("");
		textview7.setText("");
		textview8.setText("");
		textview9.setText("");
		textview10.setText("");
		if (water != null) {
			textview1.setText(water.getName());
			textview2.setText(water.getNumber());
			textview3.setText(water.getPhone());
			textview4.setText(water.getWaternumber());
			textview5.setText(water.getWateradds());
			textview6.setText(water.getWaterproperties());
			textview7.setText(water.getLastmonthwater() + "");
			textview8.setText(water.getLastmonthwater() + "");
			if (water.getCopystatus().equals("已复抄")) {
				textview9.setText(water.getThismonthwater() + "");
			} else {
				textview9.setText("");
			}

			textview10.setText(water.getCopystatus());

		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, ITEM0, 0, "二次复抄");
		menu.add(0, ITEM0 + 1, 0, "百度地图");
		menu.add(0, ITEM0 + 2, 0, "历史用水量");
		menu.add(0, ITEM0 + 3, 0, "水表绑定");
		menu.add(0, ITEM0 + 4, 0, "RFID异常复抄");
		menu.add(0, ITEM0 + 5, 0, "RFID异常报案");

		menu.findItem(ITEM0 + 1);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (water == null) {
			Toast.makeText(ReviewWaterinfoActivity.this, "没有水表不能进行些操作",
					Toast.LENGTH_LONG).show();
			return false;
		}
		if (item.getItemId() == ITEM0) {
			if (water.getCopystatus().equals("已复抄")
					|| water.getIsupload().equals("1")) {
				Toast.makeText(ReviewWaterinfoActivity.this,
						"已经上传或已经抄过的不能设置为二抄复抄", 1000).show();
				return false;
			}
			String number = textview4.getText().toString();
			if (number != null && number.length() > 0) {
				ReviewDataDao dao = new ReviewDataDao(
						ReviewWaterinfoActivity.this);
				boolean b = dao.Modifytowcopy(number);
				if (b) {
					Toast.makeText(ReviewWaterinfoActivity.this, "操作成功",
							Toast.LENGTH_LONG).show();
					water = dao.queryOneReview(null, water.getWaterid());
					addDate();
				} else {
					Toast.makeText(ReviewWaterinfoActivity.this, "操作失败",
							Toast.LENGTH_LONG).show();
				}
				dao.close();
			} else {
				Toast.makeText(ReviewWaterinfoActivity.this, "没有水表编号不能进行些操作",
						Toast.LENGTH_LONG).show();

			}

		} else if (item.getItemId() == (ITEM0 + 1)) {
			Intent intent = new Intent(ReviewWaterinfoActivity.this,
					QueryRoutePlanMap.class);
			if (water == null) {
				Toast.makeText(ReviewWaterinfoActivity.this, "没有水表不能进行些操作",
						Toast.LENGTH_LONG).show();
				return false;
			}

			intent.putExtra("wateradds", water.getWateradds());
			intent.putExtra("latitude", water.getLatitude());
			intent.putExtra("longitude", water.getLongitude());
			startActivity(intent);
		} else if (item.getItemId() == (ITEM0 + 2)) {
			AChartEngine ace = new AChartEngine();
			if (water == null) {
				Toast.makeText(ReviewWaterinfoActivity.this, "没有水表不能进行些操作",
						Toast.LENGTH_LONG).show();
				return false;
			}
			WaterListDaoImpl dao = new WaterListDaoImpl(this);
			List<UseWaterInfoPoVo> lists = dao.queryUseWater(
					water.getWaterid(), "1");
			dao.close();
			if (lists == null || lists.size() < 1) {
				Toast.makeText(ReviewWaterinfoActivity.this, "此水表没有历史用水量信息",
						Toast.LENGTH_LONG).show();
				return false;
			}
			Intent intent = ChartFactory.getBarChartIntent(
					ReviewWaterinfoActivity.this, ace.getBarDataset(lists),
					ace.getBarRenderer(lists), Type.DEFAULT);
			startActivity(intent);
		} else if (item.getItemId() == (ITEM0 + 3)) {
			if (water == null) {
				Toast.makeText(ReviewWaterinfoActivity.this, "没有水表不能进行些操作",
						Toast.LENGTH_LONG).show();
				return false;
			}
			Intent intent = new Intent(ReviewWaterinfoActivity.this,
					WaterBandingMainActivity.class);

			intent.putExtra("waterid", water.getWaterid());
			startActivity(intent);
		} else if (item.getItemId() == (ITEM0 + 4)) {
			if (water == null) {
				Toast.makeText(ReviewWaterinfoActivity.this, "没有水表不能进行些操作",
						Toast.LENGTH_LONG).show();
				return false;
			}
			Intent intent = new Intent(ReviewWaterinfoActivity.this,
					ReviewNoRfidActivity.class);
			intent.putExtra("waterid", water.getWaterid());
			startActivity(intent);
		} else if (item.getItemId() == (ITEM0 + 5)) {
			if (water == null) {
				Toast.makeText(ReviewWaterinfoActivity.this, "没有水表不能进行些操作",
						Toast.LENGTH_LONG).show();
				return false;
			}
			Intent intent = new Intent(ReviewWaterinfoActivity.this,
					ReportNoRFIDActivity.class);
			intent.putExtra("waterid", water.getWaterid());
			startActivity(intent);
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onResume() {
		ReviewDataDao dao = new ReviewDataDao(this);
		act = (ReviewDataMainActivity) getParent();

		water = dao.queryOneReview(null, act.getWaterid());
		dao.close();
		addDate();

		super.onResume();
	}
}
